// pages/load/login/login.js
var api = require('../../../app/app.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hj_logo: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/hj.jpg',
    logo: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/logo.jpg',
    login_user: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/user.jpg',
    login_password: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/password.jpg',
    tipsStyle: 'display: none',
    loginInfo: '',
    globalData: {
      userInfo: {}
    },
    openId: '',
    avatar: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '登录'
    })
  },
  tipsClick: function () {
    this.setData({
      tipsStyle: 'display: none'
    })
  },
  toast: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },
  sumbitClick: function (e) { // 登录操作
    if (!(e.detail.value.tel && e.detail.value.password)) {
      this.toast('账号或密码不能为空')
    } else {
      if (!(/^1[3456789]\d{9}$/.test(e.detail.value.tel))) {
        this.toast('手机号码有误')
      } else {
        var that = this;
        var loginInfo = { // 获取用户信息
          tel: e.detail.value.tel,
          password: e.detail.value.password,
          openId: that.data.openId,
          avatarUrl: that.data.avatar
        }
        console.log(loginInfo)
        wx.request({
          url: api.Login,
          method: 'POST',
          data: loginInfo, //请求参数格式为json
          header: {
            'content-Type': 'application/x-www-form-urlencoded' // post请求头
          },
          success: function (res) {
            // console.log(res.data)     
            if (res.data.re == 'succ') {
              // console.log('成功')
              that.data.globalData.userInfo.userId = res.data.userone.user_id
              that.data.globalData.userInfo.avatarUrl = res.data.userone.user_headimg
              that.data.globalData.userInfo.nickName = res.data.userone.user_name
              that.data.globalData.userInfo.openId = res.data.userone.user_openid
              that.data.globalData.userInfo.phone = res.data.userone.user_iphone
              // 把获取的用户信息放在本地缓存中
              wx.setStorageSync('userReInfo', that.data.globalData.userInfo)
              that.setData({ // 清空表单
                loginInfo: ''
              })
              wx.switchTab({
                url: '/pages/home/index/index',
              })
            } else if (res.data.re == 'fail'){
              // console.log('失败')
              that.setData({ // 账号或密码错误
                tipsStyle: 'display: true'
              }) 
            } else if (res.data.re == 'diff') {
              wx.showModal({
                title: '提示',
                content: '该微信已与其他手机号绑定，请确认登录手机号是否与微信绑定手机号一致',
                success(res) {
                  if (res.confirm) {
                  } else if (res.cancel) {
                  }
                }
              })
            }
          },
          error: function (e) {
            console.log(e)
          }
        })
      }
    }
  },
  registeClick: function () { // 跳转到注册页面
    wx.navigateTo({
      url: '../registe/registe',
    })
  },
  resetClick: function () { // 跳转到重置密码页面
    wx.navigateTo({
      url: '../reset/reset',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton({ // 消除左上角的小房子
      success: function () { }
    })
    var that = this
    wx.getStorage({ // 获取本地缓存
      key: 'userReInfo',
      success(res) {
        console.log(res.data)
        that.setData({
          openId: res.data.openId,
          avatar: res.data.avatarUrl,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
